﻿
using AntdUI;
using DABLL;
using DADAL;

namespace DataAcquisition
{
    public partial class ProjectManageForm : BaseForm
    {

        #region 属性
        /// <summary>
        /// key
        /// </summary>
        private string EncryptionKey = SqlSugarHelper.EncryptionKey;

        /// <summary>
        /// 当前页码
        /// </summary>
        private int PageNumber = 1;

        /// <summary>
        /// 每页数量
        /// </summary>
        private int PageSize = 10;

        /// <summary>
        /// 总数量
        /// </summary>
        private int TotalPages;

        /// <summary>
        /// 类型 0-选择工程 1-管理工程
        /// </summary>
        public int Type = 1;
        #endregion

        #region 自定义方法
        private void SetupForm()
        {
            Table1.Height = Height - Header.Height - StackPanel1.Height - Divider1.Height - Divider2.Height - Pagination1.Height - 5;
            Table1.Columns = new AntdUI.ColumnCollection {
                new AntdUI.Column("Number","工程编号",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("Name","工程名称",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("StakeMark","工程桩号",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("PipeSpecification","管材规格",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("PipeLength","管道长度",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("DesignPressure","设计压力",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("PipeNature","管道性质",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("Testtype","试压类型",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("Company","施工单位",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("ManometerPrecision","压力表精度",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("ManometerRange", "压力表量程", AntdUI.ColumnAlign.Center),
                new AntdUI.Column("Operation","操作",AntdUI.ColumnAlign.Center){ Width="120", LineBreak=true}
            };
        }

        /// <summary>工程列表
        /// 
        /// </summary>
        public void LoadList()
        {
            List<ProjectModel> list = Project.ProjectList(PageNumber, PageSize, ref TotalPages);
            UpdateTable(list);
            Pagination1.Total = TotalPages;
            Pagination1.PageSize = PageSize;
        }

        /// <summary>更新Table
        /// 
        /// </summary>
        /// <param name="list"></param>
        private void UpdateTable(List<ProjectModel> list)
        {
            var antItemList = new List<ProjectItem>();
            foreach (ProjectModel item in list)
            {
                antItemList.Add(new ProjectItem(item.Number, item.Name, item.StakeMark, item.PipeSpecification, item.PipeLength, item.Medium, item.DesignPressure, item.PipeNature, item.TestType, item.Company, item.ManometerPrecision, item.ManometerRange, item.TemplateID));
            }
            Table1.DataSource = antItemList;
        }
        #endregion

        #region 系统方法
        public ProjectManageForm()
        {
            InitializeComponent();
            Header.Text = "试压工程管理";
            FullScreen = true;
        }

        private void ProjectManageForm_Load(object sender, EventArgs e)
        {
            SetupForm();
            Table1.Spin("正在加载中...", (config) =>
            {
                LoadList();
            });
        }
        private void AddButton_Click(object sender, EventArgs e)
        {
            ProjectAddForm frm = new ProjectAddForm();
            frm.Owner = this;
            frm.ShowDialog();
        }

        private void Table1_CellButtonClick(object sender, AntdUI.TableButtonEventArgs e)
        {
            if (e.Record is ProjectItem data)
            {
                CellLink link = e.Btn;
                if (link.Id == "edit")
                {
                }
                else if (link.Id == "delete")
                {
                    string msg = string.Format("您确定要删除【{0}】工程吗？", data.Name);
                    AntdUI.Modal.open(new AntdUI.Modal.Config(this, "友情提示", msg)
                    {
                        OnOk = config =>
                        {
                            Response response = Project.DeleteProject(data.Number);
                            if (response.Code != 200)
                            {
                                Error(response.Msg);
                            }
                            else
                            {
                                Success(response.Msg);
                                Task.Run(() =>
                                {
                                    LoadList();
                                });
                            }
                            return true;
                        },
                    });
                }
            }
        }
        #endregion
    }
}
